﻿using API;
using API.Product;
using Common;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BLL
{
    public class ProductBLL : BaseBLL
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static int ProductCount() 
        {
            int count = 0;
            string sql = "select count(1) from P_Product";
            count=Convert.ToInt32(DatabaseSchema.UnitOfWork.ExecuteScalar(sql));
            return count;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DateTime MaxUpdateDate()
        {
            DateTime updateDT = DateTime.MinValue;
            string sql = "select Max(UpdateDT) from P_Product";
            updateDT = Convert.ToDateTime(DatabaseSchema.UnitOfWork.ExecuteScalar(sql));
            return updateDT;
        }
        /// <summary>
        /// 
        /// </summary>
        public void BatchInsert(DateTime? lastUpdateDate=null)
        {
            try
            {
                this.Request.LastUpdateDate = lastUpdateDate;
                ApiProduct dataProxy = ServiceProxy.ApiProductProxy();
                MP_Product[] brands = dataProxy.GetProducts(this.Request);
                if (brands == null || brands.Length <= 0) return;
                using (UnitOfWork uow = DatabaseSchema.UnitOfWork)
                {
                    foreach (MP_Product b in brands)
                    {
                        P_Product p = new P_Product(uow);
                        p.ProductID = b.ProductID;
                        p.BrandID =uow.GetObjectByKey<P_Brand>(b.BrandID);
                        p.CategoryID = uow.GetObjectByKey<P_Category>(b.CategoryID);
                        p.ProductCode = b.ProductCode;
                        p.ProductName = b.ProductName;
                        p.ProductSpec = b.ProductSpec;
                        p.BarCode = b.BarCode;
                        p.StatusID = b.StatusID;
                        p.SalePrice=b.SalePrice;
                        p.SalePriceLowest=b.SalePriceLowest;
                        p.CostPrice=b.CostPrice;
                        p.Sort = b.Sort;
                        p.Creator = b.Creator;
                        p.CreateDT = b.CreateDT;
                        p.Operator = b.Operator;
                        p.UpdateDT = b.UpdateDT.Value;
                        p.Remark = b.Remark;
                        p.Save();
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ProductBLL.BatchInsert", ex);
            }
        }
        public DataTable GetProducts() 
        {
            DataTable dt = GetDataTable(new string[] { "ProductID", "ProductCode", "BrandName", "ProductName", "ProductSpec", "PurchasePrice", "SalePrice", "SalePriceLowest", "CostPrice" });
            XPCollection<P_Product> products = new XPCollection<P_Product>(DatabaseSchema.UnitOfWork);
            foreach (P_Product p in products)
            {
                DataRow dr = dt.NewRow();
                dr["ProductID"] = p.ProductID;
                dr["BrandName"] = "";
                dr["ProductCode"] = p.ProductCode;
                dr["ProductName"] = p.ProductName;
                dr["ProductSpec"] = p.ProductSpec;
                dr["SalePrice"] = p.SalePrice.ToString("f2");
                dr["SalePriceLowest"] = p.SalePriceLowest.ToString("f2");
                dr["CostPrice"] = p.CostPrice.ToString("f2");
                dt.Rows.Add(dr);
            }
            return dt;
        }

        static DataTable GetDataTable(string[] col)
        {
            DataTable dt = new DataTable();
            DataColumn column;
            foreach (string cc in col)
            {
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = cc;
                dt.Columns.Add(column);
            }
            return dt;
        }
    }
}
